package com.example.vue20220402.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.vue20220402.dao.RoleMapper;
import com.example.vue20220402.dao.UsersinfoMapper;
import com.example.vue20220402.pojo.Role;
import com.example.vue20220402.pojo.Usersinfo;
import com.example.vue20220402.service.IUsersinfoService;
import com.example.vue20220402.service.RoleService;
import com.example.vue20220402.util.ExcelUtil;
import com.example.vue20220402.vo.Constant;
import com.example.vue20220402.vo.ResultData;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author rock
 * @since 2022-04-02
 */
@Service
public class RoleServiceImpl implements RoleService {

    @Resource
    RoleMapper roleMapper;

    @Override
    public ResultData list() {
        ResultData rd = new ResultData();
        List<Role> roles = roleMapper.selectList(null);
        rd.setResult(roles);
        return rd;
    }

    @Override
    public ResultData rolelist(Role role) {
        ResultData rd = new ResultData();
        List<Role> roles = roleMapper.rolelist(role);
        rd.setResult(roles);
        return rd;
    }

    @Override
    @Transactional
    public ResultData updaterolepowers(Role role) {
        ResultData rd = new ResultData();
        //  添加 role power 关系表
        //   1  删除 角色对应的权限
        roleMapper.deleteRolePowerByRoleId(role.getRoleId());
        //  2 添加对应的role power 关系数据
        if(role.getFlist() != null && role.getFlist().size() > 0){
            roleMapper.addRolePowers(role.getFlist(), role.getRoleId());
        }
        if(role.getSlist() != null && role.getSlist().size() > 0){
            roleMapper.addRolePowers(role.getSlist(),role.getRoleId());
        }
        rd.setCode(Constant.OK.getCode());
        return rd;
    }
}
